// subcontract/Home/searchresult/searchresult.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    aliyunServrBaseURL: getApp().config.aliyunServrBaseURL,//阿里云公共地址
    tabs: ['找产品', '找厂家'],
    activeIndex: 0,//选择的下标
    page: 1,//页码
    pagingState: false,//分页状态
    goodsList: [],//商品
    screenIndex: 0,//选择的第几个筛选
    sort_by_price: '',//价格
    sort_by_sales: '',//销量
    sort_by_create: '',//新品
    keywords: '',//搜索关键词语
    factoryIndex: 0,//选择厂的下标
    pageMill: 1,//页码找厂家
    pagingStateMill: false,//分页状态  找厂家
    shopListMill: [],//厂家
  },
  /**返回 */
  navigateBack() {
    wx.navigateBack()
  },
  /**切换 */
  switch(event) {
    console.log(event);
    let { index, type } = event.currentTarget.dataset
    if (type == 'goods') {
      this.setData({
        page: 1,
        pagingState: false,
        goodsList: [],
      })
      this.setData({ screenIndex: index })
      this.getGoodsList(this.data.keywords)
    } else {
      this.setData({
        pageMill: 1,//页码找厂家
        pagingStateMill: false,//分页状态  找厂家
        shopListMill: [],//厂家
      })
      this.setData({ factoryIndex: index })
      this.getShopList(this.data.keywords)
    }

  },
  onChange(event) {
    console.log(event);
    this.setData({
      activeIndex: event.detail.index
    })
  },
  /**找厂家 */
  async getShopList(keyword) {
    let object = {
      page_no: this.data.pageMill,
      keyword,
      type: this.data.factoryIndex
    }
    if (!this.data.pagingStateMill) {
      const { code, data } = await getApp().api.getShopList(object)
      if (code == 1) {
        this.setData({
          shopListMill: this.data.shopListMill.concat(data.list)
        })
        if (data.more != 1) {
          this.setData({
            pagingStateMill: true
          })
          return
        }
        this.setData({
          pageMill: (this.data.pageMill * 1) + 1
        })
        console.log(this.data.pageMill);
      }
    }
  },
  /**商品列表 */
  async getGoodsList(keyword) {
    function toggleSort(currentSort) {
      return currentSort == 'asc' ? 'desc' : 'asc';
    }
    let index = this.data.screenIndex
    if (index == 0 || index == 1 || index == 2 || index == 3) {
      if (index == 0) {
        this.setData({
          sort_by_create: '',
          sort_by_price: '',
          sort_by_sales: ''
        })
      } else if (index == 1) {
        this.setData({
          sort_by_create: toggleSort(this.data.sort_by_create),
          sort_by_price: '',
          sort_by_sales: ''
        })
      } else if (index == 2) {
        this.setData({
          sort_by_create: '',
          sort_by_price: '',
          sort_by_sales: toggleSort(this.data.sort_by_sales),
        })
      } else if (index == 3) {
        this.setData({
          sort_by_create: '',
          sort_by_price: toggleSort(this.data.sort_by_price),
          sort_by_sales: ''
        })
      }
    }
    let object = {
      page_no: this.data.page,
      keyword,
      sort_by_price: this.data.sort_by_price, // 初始化价格排序为空
      sort_by_sales: this.data.sort_by_sales, // 初始化销量排序为空
      sort_by_create: this.data.sort_by_create, // 新品
    }
    if (!this.data.pagingState) {
      const { code, data } = await getApp().api.getGoodsList(object)
      if (code == 1) {
        this.setData({
          goodsList: this.data.goodsList.concat(data.lists)
        })
        if (data.more != 1) {
          this.setData({
            pagingState: true
          })
          return
        }
        this.setData({
          page: (this.data.page * 1) + 1
        })
        console.log(this.data.page);
      }
    }
  },
  /**
 * 跳转
 */
  navigateTo(event) {
    let url = event.currentTarget.dataset.url
    console.log(url);
    wx.navigateTo({
      url,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      keywords: options.keywords
    })
    this.getGoodsList(options.keywords)
    this.getShopList(options.keywords)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let { keywords } = this.data
    this.getGoodsList(keywords)
    this.getShopList(keywords)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})